Bitwise Operators in C Program
Bitwise operators are used for manipulating data at the bit level, also called bit level programming.
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise exclusive OR |
~ | Bitwise complement |
<< | Shift left |
>> | Shift right |
Example : pgm.c
#include <stdio.h> main() { int a = 30; /* 30 = 0001 1110 */ int b = 15; /* 15 = 0000 1111 */ int c = 0; c = a & b; /* 14 = 0000 1110 */ printf("Bitwise AND - Value of c is %d\n", c ); c = a | b; /* 31 = 0001 1111 */ printf("Bitwise OR - Value of c is %d\n", c ); c = a ^ b; /* 17 = 0001 0001 */ printf("Bitwise exclusive OR - Value of c is %d\n", c ); c = ~a; /*-31 = 1110 0001 */ printf("Bitwise complement - Value of c is %d\n", c ); c = a << 2; /* 120 = 1111 0000 */ printf("Shift left - Value of c is %d\n", c ); c = a >> 2; /* 7 = 0000 0111 */ printf("Shift right - Value of c is %d\n", c ); }
Output:
Bitwise AND - Value of c is 14
Bitwise OR - Value of c is 31
Bitwise exclusive OR - Value of c is 17
Bitwise complement - Value of c is -31
Shift left - Value of c is 120
Shift right - Value of c is 7
Bitwise OR - Value of c is 31
Bitwise exclusive OR - Value of c is 17
Bitwise complement - Value of c is -31
Shift left - Value of c is 120
Shift right - Value of c is 7